package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.model.Stop;
import lt.marsrutai.mobile.android.model.StopBookmark;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


public class StopAdapter extends ListViewAdapter<Stop> {

    String scopeFolder;
    String trackId;

    public StopAdapter(Activity activity, String scopeFolder, String trackId) {
        super(activity, R.layout.stops_item);

        this.scopeFolder = scopeFolder;
        this.trackId = trackId;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        final Stop o = getItem(position);

        if (o != null) {
            TextView stopName = (TextView) view.findViewById(R.id.stop_name);
            if (stopName != null) {
                stopName.setText(o.getName());
            }

            ImageButton mapButton = (ImageButton) view.findViewById(R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StopMapIntentHolder intentHolder = new StopMapIntentHolder(activity);
                    intentHolder.setScopeFolder(scopeFolder);
                    intentHolder.setStopId(o.getId());
                    activity.startActivity(intentHolder.getIntent());
                }
            });

            final ImageButton starButtonOff = (ImageButton) view.findViewById(R.id.star_off);
            final ImageButton starButtonOn = (ImageButton) view.findViewById(R.id.star_on);

            final StopBookmark stopBookmark = new StopBookmark();
            stopBookmark.setScope(scopeFolder);
            stopBookmark.setStopId(o.getId());
            stopBookmark.setTrackId(trackId);

            starButtonOff.setFocusable(false);
            starButtonOff.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    starButtonOff.setVisibility(View.GONE);
                    starButtonOn.setVisibility(View.VISIBLE);

                    AppContext.getBookmarkService().addStopBookmark(stopBookmark);
                }
            });

            starButtonOn.setFocusable(false);
            starButtonOn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    starButtonOn.setVisibility(View.GONE);
                    starButtonOff.setVisibility(View.VISIBLE);

                    AppContext.getBookmarkService().removeStopBookmark(stopBookmark);
                }
            });

            if (AppContext.getBookmarkService().containsStopBookmark(stopBookmark)) {
                starButtonOff.setVisibility(View.GONE);
                starButtonOn.setVisibility(View.VISIBLE);
            }
            else {
                starButtonOn.setVisibility(View.GONE);
                starButtonOff.setVisibility(View.VISIBLE);
            }
        }
        return view;
    }
}